<?php

namespace App\Services\Admin;

use App\Models\ClassBaseInfo;
use App\Models\Vip as VipModel;
use App\Models\VipInfo;
use Illuminate\Support\Facades\DB;

class Vip extends Base
{
    public static function lists()
    {
        $input = request()->all();

        $where = [];

        $where[] = ['is_display','1'];

        if(!empty($input['sear_value'])){
            $where[] = ['name','like', "%".$input['sear_value']."%"];
        }

        return VipModel::where($where)->orderBy('id', 'desc');
    }

    public static function info($id)
    {
        $vip_info = (new VipInfo)->where('vip_id', $id)->get()->toArray();

        $class_info_id = array_column($vip_info, 'class_info_id');

        $class_edit_info = ClassBaseInfo::with(['class_base', 'store', 'classsroom', 'teacher', 'quarterly', 'class_time'])->whereIn("id", $class_info_id)->get();

        return [
            'vip' => (new VipModel)->where('id', $id)->first(),
            'num' => array_column($vip_info, 'num', 'class_info_id'),
            'class_info_id' => $class_info_id,
            'class_edit_info' => $class_edit_info
        ];
    }

    public static function add($param = [])
    {
        //开启事务
        DB::beginTransaction();
        try{

            $vip_model = (new VipModel);

            $insert['name'] = $param['name'];

            $insert['duration'] = $param['duration'];

            $insert['activate'] = $param['activate'];

            $vip_id = $vip_model->insertGetId($insert);

            $class_base_info_result = explode(',', $param['class_base_info_id']);
            $num = explode(',', $param['num']);

            $class_base_info_insert = [];

            foreach ($class_base_info_result as $k=>$v){
                $input = [];
                $input['vip_id'] = $vip_id;
                $input['class_info_id'] = $v;
                $input['num'] = $num[$k]??0;

                if(empty($input['num'])){
                    $input['is_restriction'] = 0;
                }else{
                    $input['is_restriction'] = 1;
                }

                $class_base_info_insert[] = $input;
            }

            (new VipInfo())->insert($class_base_info_insert);

            DB::commit();

        }catch (\Exception $e) {

            //接收异常处理并回滚
            DB::rollBack();

            return ['bool'=>false, 'message'=>$e->getMessage()];

        }

        return ['bool'=>true, 'message'=>''];
    }

    public static function edit($id , $param=[])
    {
        //开启事务
        DB::beginTransaction();
        try{

            $vip_model = (new VipModel);

            $insert['name'] = $param['name'];

            $insert['duration'] = $param['duration'];

            $insert['activate'] = $param['activate'];

            $vip_model->where('id', $id)->update($insert);

            (new VipInfo())->where('vip_id', $id)->delete();

            $class_base_info_result = explode(',', $param['class_base_info_id']);

            $num = explode(',', $param['num']);

            $class_base_info_insert = [];

            foreach ($class_base_info_result as $k=>$v){
                $input = [];
                $input['vip_id'] = $id;
                $input['class_info_id'] = $v;
                $input['num'] = $num[$k]??0;

                if(empty($input['num'])){
                    $input['is_restriction'] = 0;
                }else{
                    $input['is_restriction'] = 1;
                }

                $class_base_info_insert[] = $input;
            }

            (new VipInfo())->insert($class_base_info_insert);

            DB::commit();

        }catch (\Exception $e) {

            //接收异常处理并回滚
            DB::rollBack();

            return ['bool'=>false, 'message'=>$e->getMessage()];

        }

        return ['bool'=>true, 'message'=>''];
    }

    public static function del($id)
    {
        (new VipModel)->where('id', $id)->update(["is_display"=>'-1']);
    }

    public static function user_coupon_record($id)
    {
        return UserCouponRecord::where('coupon_id', $id);
    }

    public static function class_base_info()
    {
        $input = request()->all();

        $where = [];

        if (!empty($input['classroom'])) $where[] = ['class_classroom_id', $input['classroom']];
        if (!empty($input['store_id'])) $where[] = ['store_id', $input['store_id']];

        if(!empty($input['start_hour']) && empty($input['start_minute'])){
            $where[] = ['class_time', 'like', "{$input['start_hour']}%"];
        }

        if(!empty($input['start_hour']) && !empty($input['start_minute'])){
            $where[] = ['class_time', 'like', "{$input['start_hour']}:{$input['start_minute']}%"];
        }

        $result = ClassBaseInfo::with(['class_base', 'store', 'classsroom', 'teacher', 'quarterly', 'class_time', 'class_base_info_teacher' =>function($query){
            $query->with(['teacher']);
        }])->whereHas('quarterly',function($query) use ($input){
            if(!empty($input['start_time']) and !empty($input['end_time'])){
                $where[] = ['start_time',">=", $input['start_time']];
                $where[] = ['start_time',"<=", $input['end_time']];
                $query->where($where);
            }
        });

        if (!empty($input['teacher'])){
            $result = $result->whereHas('class_base_info_teacher', function ($query) use ($input){
                if(!empty($input['teacher'])) {
                    $where[] = ["teacher_id", "=", $input['teacher']];
                    $query->with(['teacher'])->where($where);
                }
            });
        }

        return $result->where($where)->orderBy('id', 'desc')->get();
    }
}
